package flint04
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Rectangle;
	
	import org.flintparticles.common.events.ParticleEvent;
	import org.flintparticles.twoD.emitters.Emitter2D;
	import org.flintparticles.twoD.particles.Particle2D;
	import org.flintparticles.twoD.renderers.PixelRenderer;

	[SWF(width='461', height='574', frameRate='61', backgroundColor='#000000')]
	
	public class Main extends Sprite{
		
		private const XML_FILE:String = "imageData.xml";
		private const PARTICLE_COUNT:uint = 2500;
		private var bitmapLoader:BitmapLoader;

		private var emitterBlast:Emitter2D;
		private var emitterOne:Emitter2D;
		private var emitterTwo:Emitter2D;
		
		private var index:int = 0;
		private var renderer:PixelRenderer;

		public function Main(){
			bitmapLoader = new BitmapLoader(XML_FILE);
			bitmapLoader.addEventListener(BitmapLoader.BITMAPS_LOADED, initEmitters);
			bitmapLoader.initXML();			
		}
		
		private function initEmitters(event:Event):void{
			emitterBlast = new EmitterBlast(PARTICLE_COUNT,bitmapLoader.getNextBitmap(),bitmapLoader.getNextBitmap());
			emitterOne = new EmitterOne(bitmapLoader.getNextBitmap());
			emitterTwo = new EmitterTwo(bitmapLoader.getNextBitmap());
			
			renderer = new PixelRenderer( new Rectangle( 0, 0, 461, 574 ) );
			renderer.addFilter(new BlurFilter(2, 2, 1));
			renderer.addFilter(new ColorMatrixFilter([1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0.95,0]));
			renderer.addEmitter(emitterBlast);
			renderer.addEmitter(emitterOne);
			renderer.addEmitter(emitterTwo);
			addChild(renderer);

			emitterBlast.addEventListener(ParticleEvent.PARTICLE_DEAD, animateTo1);
			emitterOne.addEventListener(ParticleEvent.PARTICLE_DEAD, animateTo2);
			emitterTwo.addEventListener(ParticleEvent.PARTICLE_DEAD, animateTo1);
			
			emitterBlast.start();
			emitterOne.start();
			emitterTwo.start();
		}
		
		
		
		private function animateTo1(event:ParticleEvent):void{
			index++;
			if(index == PARTICLE_COUNT){
				emitterTwo = new EmitterTwo(bitmapLoader.getNextBitmap());
				renderer.addEmitter(emitterTwo);
				emitterTwo.addEventListener(ParticleEvent.PARTICLE_DEAD, animateTo1);
				emitterTwo.start();
				index = 0;
			}
			
			var deadParticle:Particle2D = Particle2D(event.particle);
			var bornParticle:Particle2D = Particle2D(emitterOne.particleFactory.createParticle());
			bornParticle.x = deadParticle.x;
			bornParticle.y = deadParticle.y;
			bornParticle.color = deadParticle.color;			
			emitterOne.addExistingParticles([bornParticle], true);
			
		}
		
		private function animateTo2(event:ParticleEvent):void{
			index++;
			if(index == PARTICLE_COUNT){
				emitterOne = new EmitterOne(bitmapLoader.getNextBitmap());
				renderer.addEmitter(emitterOne);
				emitterOne.addEventListener(ParticleEvent.PARTICLE_DEAD, animateTo2);
				emitterOne.start();
				index = 0;
			}
			
			var deadParticle:Particle2D = Particle2D(event.particle);
			var bornParticle:Particle2D = Particle2D(emitterTwo.particleFactory.createParticle());
			bornParticle.x = deadParticle.x;
			bornParticle.y = deadParticle.y;
			bornParticle.color = deadParticle.color;			
			emitterTwo.addExistingParticles([bornParticle ], true);
		}
		
		
	}
}